<?php
namespace app\common\controller;
use think\Controller;
abstract class Base extends Controller{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 从网页接收提交数据
     * @param array $params 需要接收的数据[0=>'user_name']/['username'=>'user_name/s']
     * @param string $method 获取方式get/post/input
     * @return array
     */
    protected function buildParams($params = [], $method = 'param')
    {
        $result_data = [];
        if (empty($params))
        {
            return $this->request->$method();
        } else if (is_array($params) && !empty($params))
        {
            foreach ($params as $index => $item)
            {
                if(is_array($item) && isset($item['callback']) && is_callable($item['callback']))
                {
                    $result_data[$index] =  call_user_func($item['callback'],$index);
                }
                else if (is_numeric($index))
                {
                    $result_data[$item] = $this->protectStr($this->request->$method($item));
                } else
                {
                    $result_data[$index] = $this->protectStr(($this->request->$method($item)));
                }
            }
        } else if (is_string($params))
        {
            $result_data = $this->protectStr($this->request->$method($params));
        }
        return $result_data;
    }
}